package org.xyl.game.controller;


import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.xyl.game.entity.Point;
import org.xyl.game.service.PointService;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

@Slf4j
@Controller
public class TestController {
    @Autowired
    private PointService pointService;

    @PostMapping("/save")
    @ResponseBody
    public String save(String acceleration) {
        log.info(acceleration);
        pointService.savePoint(Double.parseDouble(acceleration));
        return "ok";
    }

    @PostMapping("login.jsp")
    @ResponseBody
    public String login(String data) {


        String[] userinfo = data.split(",");
        String username = userinfo[0];
        String passwd = userinfo[1];
        try {
            Class.forName("com.mysql.jdbc.Driver");
            Connection con = DriverManager.getConnection("jdbc:mysql://192.168.10.112:3306/app", "root", "Root123!");
            Statement state = con.createStatement();
            String sql = "select passwd,userid from userinfo where username='" + username + "'";
            ResultSet rs = state.executeQuery(sql);
            rs.next();
            String rsPasswd = rs.getString("passwd");
            String rsUserid = rs.getString("userid");
            state.close();
            con.close();
            if (passwd.equals(rsPasswd))
                return "success" + "," + rsUserid + "," + username + ",";
            else
                return "failed";


        } catch (Exception e) {
            //e.printStackTrace();
            return "failed";
        }
    }
    @PostMapping("signin.jsp")
    @ResponseBody
    public String signin(String data){


            String [] userinfo = data.split(",");
            String username = userinfo[0];
            //String username = new String(username1.getBytes("ISO-8859-1"),"GB2312");
            String passwd = userinfo[1];
            //out.print("username:"+username+",passwd:"+passwd);
            try{
                Class.forName("com.mysql.jdbc.Driver");
                Connection con = DriverManager.getConnection("jdbc:mysql://192.168.10.112:3306/app", "root", "Root123!");
                Statement state=con.createStatement();
                String sql1 = "insert into userinfo(username,passwd) values('"+username+"','"+passwd+"')";
                //out.println(sql1);
                //String sql1 = "insert into userinfo(username,passwd) values('liugang','1qasw2')";
                state.executeUpdate(sql1);
                String sql2 = "select userid,username from userinfo where username='"+username+"'";
                ResultSet rs=state.executeQuery(sql2);
                rs.next();
                String rsUserid=rs.getString("userid");
                String rsUsername=rs.getString("username");
                state.close();
                con.close();
                if(rsUserid != null)
                   return "success,"+rsUserid+","+rsUsername+",";
                else
                    return "failed";


            }
            catch (Exception e) {
                e.printStackTrace();
                return "failed";
                //out.print("username:"+username+",passwd:"+passwd);
            }


    }
}
